//
//  NoteDao.m
//  MyNotes
//
//  Created by chen on 17/3/15.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "NoteDao.h"

@interface NoteDao()
@property(nonatomic,strong) NSDateFormatter *dateFormatter;
@property(nonatomic,strong) NSString *plistPath;
@end

@implementation NoteDao
static NoteDao *shareSingleton = nil;
//单例模式
+(NoteDao *)sharedInstance{
    static dispatch_once_t once;
    dispatch_once(&once, ^{
        shareSingleton = [[self alloc]init];
        //初始化沙盒目录中的属性列表
        shareSingleton.plistPath = [shareSingleton applicationDocumentDirectoryFile];
        //添加一些测试数据
        shareSingleton.dateFormatter = [[NSDateFormatter alloc]init];
        [shareSingleton.dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        //初始化属性列表文件
        [shareSingleton createEditableCopyOfDatabaseIfNeed];
    });
    
    return shareSingleton;
}
//初始化属性文件的元素
-(void)createEditableCopyOfDatabaseIfNeed{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL DBexits = [fileManager fileExistsAtPath:self.plistPath];
    if (!DBexits) {
        NSBundle *frameworkBundle = [NSBundle bundleForClass:[NoteDao class]];
        NSString *frameworkBundlePath = [frameworkBundle resourcePath];
        NSString *defaultDBPath = [frameworkBundlePath stringByAppendingPathComponent:@"NotesList.plist"];
        NSError *error;
        BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:_plistPath error:&error];
        NSAssert(success, @"错误写入文件");
    }
}
//获取plist的path路径
-(NSString *)applicationDocumentDirectoryFile{
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, TRUE) lastObject];
    NSString *path = [documentDirectory stringByAppendingPathComponent:@"Noteslist.plist"];
    //测试sqlite3使用的数据源
//    CommonDao *commonDao = [CommonDao new];
//    [commonDao openDatabase];
//    if (![commonDao readData]) {
//         [commonDao insertData];
//    }
    //测试FMDB的sqlite3数据源
//    FMDBDao *fm = [FMDBDao new];
//    [fm openDatabase];
//    if (![fm readData]) {
//        [fm insertData];
//    }
//    [fm updateData:100];
//    [fm deleteData:1000];
//    [fm readData];
    return path;
}

-(int)create:(Note *)model{
    NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:_plistPath];
    NSString *strDate = model.dateString;
    NSDictionary *dict = @{@"date":strDate,@"content":model.content};
    [array addObject:dict];
    [array writeToFile:_plistPath atomically:TRUE];
    return 0;
}
-(int)remove:(Note *)model{
    NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:_plistPath];
    for (NSDictionary *dict in array) {
        NSString *strDate = dict[@"date"];
        if ([strDate isEqualToString:model.dateString]) {
            [array removeObject:dict];
            [array writeToFile:_plistPath atomically:TRUE];
            break;
        }
    }
    return 0;
}
-(int)modify:(Note *)model{
    NSArray *array = [[NSArray alloc]initWithContentsOfFile:_plistPath];
    for (NSDictionary *dict in array) {
        NSString *strDate = dict[@"date"];
        if ([strDate isEqualToString:model.dateString]) {
            [dict setValue:model.content forKey:@"content"];
            [array writeToFile:_plistPath atomically:TRUE];
            break;
        }
    }
    return 0;
}
-(NSMutableArray *)findAll{
    NSArray *array = [[NSArray alloc]initWithContentsOfFile:_plistPath];
    NSMutableArray *listData = [[NSMutableArray alloc]init];
    for (NSDictionary *dict in array) {
        NSString *strDate = dict[@"date"];
        NSString *content = dict[@"content"];
        Note *note = [[Note alloc]initWithDate:[_dateFormatter dateFromString:strDate] content:content];
        [listData addObject:note];
    }
    return listData;
}

-(Note *)findById:(Note *)model{
    NSArray *array = [[NSArray alloc]initWithContentsOfFile:_plistPath];
    for (NSDictionary *dict in array) {
        NSString *strDate = dict[@"date"];
        NSString *content = dict[@"content"];
        if ([strDate isEqualToString:model.dateString]) {
            Note *note = [[Note alloc]initWithDate:[_dateFormatter dateFromString:strDate] content:content];
            return note;
        }
    }
    return nil;
}
@end
